//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SwapRateTrigger.h"
using namespace Cephei::QL::Models::Marketmodels::Callability;
#include <gen/QL/Models/Marketmodels/CurveState.h>
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::CSwapRateTrigger (Cephei::Core::IVector<Double>^ rateTimes, Cephei::Core::IVector<Double>^ swapTriggers, Cephei::Core::IVector<Double>^ exerciseTimes) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phSwapRateTrigger = NULL;
#endif
        rateTimes->Lock();
        INativeVector<Double>^ _NCIrateTimes = rateTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrateTimes = safe_cast<CDoubleVector^>(_NCIrateTimes);
        std::vector<QuantLib::Time>& _rateTimes = static_cast<std::vector<QuantLib::Time>&> (_NCrateTimes->GetReference ());
        swapTriggers->Lock();
        INativeVector<Double>^ _NCIswapTriggers = swapTriggers->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCswapTriggers = safe_cast<CDoubleVector^>(_NCIswapTriggers);
        std::vector<QuantLib::Rate>& _swapTriggers = static_cast<std::vector<QuantLib::Rate>&> (_NCswapTriggers->GetReference ());
        exerciseTimes->Lock();
        INativeVector<Double>^ _NCIexerciseTimes = exerciseTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCexerciseTimes = safe_cast<CDoubleVector^>(_NCIexerciseTimes);
        std::vector<QuantLib::Time>& _exerciseTimes = static_cast<std::vector<QuantLib::Time>&> (_NCexerciseTimes->GetReference ());
        _ppSwapRateTrigger = new boost::shared_ptr<QuantLib::SwapRateTrigger> (new QuantLib::SwapRateTrigger ( _rateTimes,  _swapTriggers,  _exerciseTimes ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (rateTimes != nullptr) rateTimes->Unlock();    //not optional
        if (swapTriggers != nullptr) swapTriggers->Unlock();    //not optional
        if (exerciseTimes != nullptr) exerciseTimes->Unlock();    //not optional
    }
}
Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::CSwapRateTrigger (boost::shared_ptr<QuantLib::SwapRateTrigger>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSwapRateTrigger = NULL;
#endif
	_ppSwapRateTrigger = &childNative;
    
}
Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::CSwapRateTrigger (QuantLib::SwapRateTrigger& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phSwapRateTrigger = NULL;
#endif
	_ppSwapRateTrigger = new boost::shared_ptr<QuantLib::SwapRateTrigger> (&childNative);
    
    _SwapRateTriggerOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::CSwapRateTrigger (CSwapRateTrigger^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSwapRateTrigger = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSwapRateTrigger = new boost::shared_ptr<QuantLib::SwapRateTrigger> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::CSwapRateTrigger (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSwapRateTrigger = NULL;
#endif
	if (!t->IsSubclassOf(CSwapRateTrigger::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::CSwapRateTrigger (QuantLib::Handle<QuantLib::SwapRateTrigger>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSwapRateTrigger = &childNative;
	_ppSwapRateTrigger = &static_cast<boost::shared_ptr<QuantLib::SwapRateTrigger>>(childNative.currentLink());
    
    _SwapRateTriggerOwner = owner;
}
Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::CSwapRateTrigger (QuantLib::Handle<QuantLib::SwapRateTrigger> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSwapRateTrigger = &childNative;
	_ppSwapRateTrigger = &static_cast<boost::shared_ptr<QuantLib::SwapRateTrigger>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::CSwapRateTrigger (QuantLib::SwapRateTrigger childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSwapRateTrigger = NULL;
#endif
	_ppSwapRateTrigger = new boost::shared_ptr<QuantLib::SwapRateTrigger> (new QuantLib::SwapRateTrigger (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::~CSwapRateTrigger ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppSwapRateTrigger != NULL)
    {
	    delete _ppSwapRateTrigger;
        _ppSwapRateTrigger = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::!CSwapRateTrigger ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppSwapRateTrigger != NULL)
    {
	    delete _ppSwapRateTrigger;
    }
}
QuantLib::SwapRateTrigger& Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::GetReference ()
{
    if (_ppSwapRateTrigger == NULL) throw REFNEW NativeNullException ();
	return **_ppSwapRateTrigger;
}
boost::shared_ptr<QuantLib::SwapRateTrigger>& Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::GetShared ()
{
    if (_ppSwapRateTrigger == NULL) throw REFNEW NativeNullException ();
	return *_ppSwapRateTrigger;
}
QuantLib::SwapRateTrigger* Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::GetPointer ()
{
    if (_ppSwapRateTrigger == NULL) throw REFNEW NativeNullException ();
	return &**_ppSwapRateTrigger;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SwapRateTrigger>& Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::GetHandle ()
{
	if (_phSwapRateTrigger == NULL)
	{
		_phSwapRateTrigger = new Handle<QuantLib::SwapRateTrigger> (*_ppSwapRateTrigger);
	}
	return *_phSwapRateTrigger;
}
#endif
bool Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::HasNative () 
{
	return (_ppSwapRateTrigger != NULL);
}

Boolean Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::Exercise (Cephei::QL::Models::Marketmodels::ICurveState^ currentState)
{
    CCurveState^ _CcurrentState;
    try
    {
        _CcurrentState = safe_cast<CCurveState^> (currentState);
        _CcurrentState->Lock();
        QuantLib::CurveState& _currentState = static_cast<QuantLib::CurveState&> (_CcurrentState->GetReference ()); 
    	bool _rv = (bool)(*_ppSwapRateTrigger)->exercise ( _currentState );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcurrentState != nullptr) _CcurrentState->Unlock();
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::ExerciseTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppSwapRateTrigger)->exerciseTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Callability::ISwapRateTrigger^ Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::NextStep (Cephei::QL::Models::Marketmodels::ICurveState^ currentState)
{
    CCurveState^ _CcurrentState;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CcurrentState = safe_cast<CCurveState^> (currentState);
        _CcurrentState->Lock();
        QuantLib::CurveState& _currentState = static_cast<QuantLib::CurveState&> (_CcurrentState->GetReference ()); 
    	(*_ppSwapRateTrigger)->nextStep ( _currentState );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcurrentState != nullptr) _CcurrentState->Unlock();
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::RelevantTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppSwapRateTrigger)->relevantTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Callability::ISwapRateTrigger^ Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppSwapRateTrigger)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Callability::ISwapRateTrigger^ Cephei::QL::Models::Marketmodels::Callability::CSwapRateTrigger_Factory::Create (Cephei::Core::IVector<Double>^ rateTimes, Cephei::Core::IVector<Double>^ swapTriggers, Cephei::Core::IVector<Double>^ exerciseTimes)
{
    return REFNEW CSwapRateTrigger ( rateTimes,  swapTriggers,  exerciseTimes);
}
